package com.wzt.lianfirecontrol.adapter.home.system.baojing;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.App;
import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.home.system.SysDeviceModel;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.ToastUtils;
import com.wzt.lianfirecontrol.utils.Utils;

public class SysBaoJingDeviceAdapter extends BaseRecyclerAdapter<SysDeviceModel> {
    private Context context;
  //  private OnItemClickListener mOnItemClickListener;
    private boolean needSetTopMargin = false;
  //  private OnItemClickListener<SysDeviceModel> mListener;

/*    public void setOnItemClickListener(OnItemClickListener<SysDeviceModel> li) {
        mListener = li;
    }*/
    private OnChildClickListener onItemClickListener;
    public boolean isNeedSetTopMargin() {
        return needSetTopMargin;
    }

    public void setNeedSetTopMargin(boolean needSetTopMargin) {
        this.needSetTopMargin = needSetTopMargin;
    }

    public SysBaoJingDeviceAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_info_list, parent, false);

        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, SysDeviceModel data) {
        if (viewHolder instanceof MyViewHolder) {
            setItemData((MyViewHolder) viewHolder, data, RealPosition);
        }

    }

    private class MyViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout ll_item_content;
        private ImageView iv_title;
        private TextView tv_title;
        private TextView tv_flag;
        private TextView tv_status;
        private ImageView iv_show_goto;
        private LinearLayout ll_location;
        private ImageView iv_location;
        private TextView tv_location;
        private LinearLayout ll_time;
        private ImageView iv_time;
        private TextView tv_time;
        private ImageView iv_videos;


        public MyViewHolder(View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.iv_title = itemView.findViewById(R.id.iv_title);
        myViewHolder.tv_title = itemView.findViewById(R.id.tv_title);
        myViewHolder.tv_flag = itemView.findViewById(R.id.tv_flag);
        myViewHolder.tv_status = itemView.findViewById(R.id.tv_status);
        myViewHolder.iv_show_goto = itemView.findViewById(R.id.iv_show_goto);
        myViewHolder.ll_location = itemView.findViewById(R.id.ll_location);
        myViewHolder.iv_location = itemView.findViewById(R.id.iv_location);
        myViewHolder.tv_location = itemView.findViewById(R.id.tv_location);
        myViewHolder.ll_time = itemView.findViewById(R.id.ll_time);
        myViewHolder.iv_time = itemView.findViewById(R.id.iv_time);
        myViewHolder.tv_time = itemView.findViewById(R.id.tv_time);
        myViewHolder.iv_videos = itemView.findViewById(R.id.iv_videos);
    }

    private void setItemData(MyViewHolder viewHolder, SysDeviceModel data, int position) {

        final int pos = getRealPosition(viewHolder);

        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.ll_item_content.getLayoutParams();
        if (0 == position && needSetTopMargin) {
            layoutParams.setMargins(Utils.dip2px(context, 0), Utils.dip2px(context, 15),
                    Utils.dip2px(context, 0), Utils.dip2px(context, 15));
        } else {
            layoutParams.setMargins(Utils.dip2px(context, 0), Utils.dip2px(context, 0),
                    Utils.dip2px(context, 0), Utils.dip2px(context, 15f));
        }

        String haveVideoMonitor = data.getHaveVideoMonitor();
        if (haveVideoMonitor != null) {
            if (haveVideoMonitor.equals("0")) {
                viewHolder.iv_videos.setVisibility(View.GONE);
            } else {
                viewHolder.iv_videos.setVisibility(View.VISIBLE);
            }
        }

        viewHolder.iv_videos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //    mListener.onItemClick(v,pos,data);
            //    onItemClickListener.success(pos);
          //      ToastUtils.showToast(App.context,"视频");
                mListener.onItemClick(v, pos, data);
            }
        });

        if (StringUtils.isEmpty(data.getName())) {
            viewHolder.tv_title.setVisibility(View.GONE);
        } else {
            viewHolder.tv_title.setVisibility(View.VISIBLE);
            viewHolder.tv_title.setText(data.getName());
        }

        if (StringUtils.isEmpty(data.getRunStatus())) {
            viewHolder.tv_flag.setVisibility(View.GONE);
        } else {
            viewHolder.tv_flag.setVisibility(View.VISIBLE);
            viewHolder.tv_flag.setText(Utils.changeStatusIdToStr(data.getRunStatus()));
            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setColor(Color.parseColor(Utils.changeStatusIdToColor(data.getRunStatus())));
            gradientDrawable.setCornerRadius(Utils.dip2px(context, 10));
            viewHolder.tv_flag.setBackground(gradientDrawable);
        }

        StringBuilder stringBuilder = new StringBuilder();
        if (!StringUtils.isEmpty(data.getBuildingName())) {
            stringBuilder.append(data.getBuildingName());
        }
        if (!StringUtils.isEmpty(data.getFloorName())) {
            stringBuilder.append(data.getFloorName());
        }
        if (!StringUtils.isEmpty(data.getAddress())) {
            stringBuilder.append(data.getAddress());
        }

        if (StringUtils.isEmpty(stringBuilder.toString())) {
            viewHolder.ll_location.setVisibility(View.GONE);
        } else {
            viewHolder.ll_location.setVisibility(View.VISIBLE);
            viewHolder.iv_location.setVisibility(View.VISIBLE);
            viewHolder.tv_location.setText(stringBuilder.toString());
        }

        if (StringUtils.isEmpty(data.getWarningTimeStr())) {
            if (StringUtils.isEmpty(data.getCreateTime())) {
                viewHolder.ll_time.setVisibility(View.GONE);
            } else {
                viewHolder.ll_time.setVisibility(View.VISIBLE);
                viewHolder.iv_time.setVisibility(View.VISIBLE);
                viewHolder.tv_time.setText(data.getCreateTime());
            }
        } else {
            viewHolder.ll_time.setVisibility(View.VISIBLE);
            viewHolder.iv_time.setVisibility(View.VISIBLE);
            viewHolder.tv_time.setText(data.getWarningTimeStr());
        }
        viewHolder.iv_show_goto.setVisibility(View.VISIBLE);
    }
    public void setOnChildPositionListener(OnChildClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnChildClickListener{
        //成功的方法传 int 的索引
        void success(int index);
    }
//    public interface OnItemClickListener<T> {
//        void onItemClick(View v, int position, T data);
//    }

/*    public interface OnItemChildClickListener {
        void onChildClicked(int position);
    }


    public void setOnItemChildClickListener(OnItemClickListener clickListener) {
        this.mOnItemClickListener = clickListener;
    }*/

}
